home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17456 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: in2.uu.net!zdc!szdc!news
  2. From: braz@ime.usp.br (Rodrigo de Salvo Braz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why?  Virtual member causes General Protection Exception
  5. Date: Mon, 15 Apr 1996 17:05:34 GMT
  6. Organization: Zippo
  7. Message-ID: <4ku0vr$lg3@clark.zippo.com>
  8. References: <31730432.28C@es.flinders.edu.au>
  9. NNTP-Posting-Host: ddata116.dialdata.com.br
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. "T. Gruber" <gruber@es.flinders.edu.au> wrote:
  13.  
  14. >Hi
  15.  
  16. >I can compile and run the appended code (BC++ 4.5; Win 3.11), but it causes a 
  17. >General Protection Exception in the last line. Why? 
  18.  
  19. >A::y() does call B::x(), but this code does not get the right address for B::i. I 
  20. >assume I am at fault, but I can't find a documented reference that tells me so. Am 
  21. >*I* stupid?
  22.  
  23. >Please email a copy of your reply.
  24.  
  25. >Thanks
  26.  
  27. >Thomas
  28.  
  29. >#include <iostream.h>
  30.  
  31. >class A {
  32. >public:
  33. >  virtual void x() = 0;
  34. >  void y() { x(); };
  35. >};
  36.  
  37. >class B : public virtual A {
  38. >public:
  39. >  B() : i(0) {};
  40. >  virtual void x() { cout << i << endl; };
  41. >  int i;
  42. >};
  43.  
  44. >class C : public virtual A, public virtual B {};
  45.  
  46. >void main()
  47. >{
  48. >  C c1;
  49. >  C c2(c1);
  50. >  c2.x();  // works
  51. >  c2.y();  // crashes in B::x()
  52. >}
  53.  
  54. If you had this problem this exactly way, I don't know how to help
  55. you. But if the code is divided in modules, I have had the same kind
  56. of problem. The fact is that virtual tables, which contain addresses
  57. for virtual member functions, get those addresses only at run-time. So
  58. you begin you program execution without correct addresses there! When
  59. you get at main function, you have them already, but by now some
  60. constructor may have been run and crashed if it tried to use those
  61. virtual member function.
  62.  
  63. Good luck,
  64.  
  65. Rodrigo Braz
  66.  
  67.  
  68.